package com.dt.dubai.mber.controller;

import com.dt.dubai.mber.entity.MberCustomerAddrEntity;
import com.dt.dubai.mber.controller.innate.MberCustomerAddrInnateAbs;
import com.dt.dubai.core.comm.*;
import com.dt.dubai.core.utile.DateTools;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import java.util.Date;
import java.util.List;

/*
* 顾客地址表
*/

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.TypeReference;
import java.lang.reflect.Type;
import com.dt.dubai.core.exception.PageErrorMesg;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import java.util.Map;

@Controller
@RequestMapping(value = "/mber/mberCustomerAddr")
public class MberCustomerAddrController extends MberCustomerAddrInnateAbs {

    /*innateServiceStart*/
    @Override
    protected void userSavEntity(MberCustomerAddrEntity inEntity, final Long userId) {
        inEntity.setDelSign(false);
    }

    @Override
    protected void userModEntity(MberCustomerAddrEntity inEntity, final Long userId) {
    }

    @Override
    protected void userDelByKey(List<Long> recordIds, final Long userId) {
    }

    @Override
    protected void userQueByKey(Long recordId, Map<String, Object> map, final Long userId) {
        if (null != map && map.size() > 0) {
            map.put("createTxStamp", DateTools.dateFormat((Date) map.get("createTxStamp"), DateTools.DATE_FORMAT_YMD));
            map.remove("customerRecordId");
            map.remove("delSign");
            map.remove("lastupTxStamp");
        }
    }

    @Override
    protected void userQuePageList(PageParameterData queryCondition, List<PageCondition> joinCondition, List<Map<String, Object>> resultMap, final Long userId) {
        for (Map<String, Object> map : resultMap) {
            map.put("createTxStamp", DateTools.dateFormat((Date) map.get("createTxStamp"), DateTools.DATE_FORMAT_YMD));
            Boolean defaultAddress = null == map.get("defaultAddress") ? false : (Boolean) map.get("defaultAddress");
            if (defaultAddress) {
                map.put("defaultAddress", defaultAddress);
                map.put("defaultAddressStr", "是");
            } else {
                map.put("defaultAddress", defaultAddress);
                map.put("defaultAddressStr", "否");
            }
            map.remove("delSign");
            map.remove("lastupTxStamp");
        }
    }

    /*innateServiceEnd*/
    /*以上内容请误删除或是移动，系统升级使用，用户代码请写入下方*/

    @RequestMapping(value = "/findListByUserId", method = RequestMethod.POST)
    @ResponseBody
    public PageData<String> findFindListByUserId(@RequestParam("data")  String data, final Long userId) {
        // 查询当前用户所有地址 
        try {
            if (null == data) {
                SqlUtils.clearSqlCode(data);
                SqlUtils.clearWebCode(data);
             }
            Type type = new TypeReference<Map<String, Object>>() {
            }.getType();
            Map<String, Object> map = JSON.parseObject(data, type);
            Map<String, Object> result = this.mberCustomerAddrSer.findFindListByUserId(map, userId);
            return new PageData().success(result, i18nUtile.getMessage(PageErrorMesg.E0.name()));
        } catch (RuntimeException e) {
            e.printStackTrace();
            return PageData.error(PageErrorMesg.E50.name(), i18nUtile.getMessage(PageErrorMesg.E50.name()));
        }
    }


    //修改默认地址
    @RequestMapping(value = "/updateDefaultAddress", method = RequestMethod.GET)
    @ResponseBody
    public PageData<String> updateDefaultAddress(Long newRecordId,Long oldRecordId){
        try {
            mberCustomerAddrSer.updateDefaultAddress(newRecordId,oldRecordId);
            return new PageData().success("sucess", i18nUtile.getMessage(PageErrorMesg.E0.name()));
        } catch (RuntimeException e) {
            String code = e.getMessage() == null ? "E911" : e.getMessage().startsWith("E") ? e.getMessage() : "E911";
            System.out.println(i18nUtile.getMessage(code));
            e.printStackTrace();
            return PageData.error(code, i18nUtile.getMessage(code));
        }
    }

}
